//
//  TJPhotosTool.m
//  BaoDian
//
//  Created by mac on 2024/8/7.
//

#import "TJPhotosTool.h"

@implementation TJPhotosTool

+ (void)fetchImageWithLocalIdentifier:(NSString *)localIdentifier completion:(void (^)(UIImage *image))completion {
    PHFetchResult<PHAsset *> *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil];
    if (fetchResult.count > 0) {
        PHAsset *asset = fetchResult.firstObject;
        
        PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
        requestOptions.synchronous = NO;
        requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        
        [[PHImageManager defaultManager] requestImageForAsset:asset
                                                   targetSize:PHImageManagerMaximumSize
                                                  contentMode:PHImageContentModeAspectFit
                                                      options:requestOptions
                                                resultHandler:^(UIImage *result, NSDictionary *info) {
            if (completion) {
                completion(result);
            }
        }];
    } else {
        if (completion) {
            completion(nil);
        }
    }
}

+ (void)saveVideoToPhotoLibrary:(NSURL *)videoURL completion:(void (^)(NSError *error, NSString *localIdentifier))completion {
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status != PHAuthorizationStatusAuthorized) {
            NSLog(@"用户拒绝访问相册");
            if (completion) {
                completion(nil, nil);
            }
            return;
        }
        
        __block NSString *localIdentifier = nil;
        
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetChangeRequest *creationRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoURL];
            PHObjectPlaceholder *assetPlaceholder = [creationRequest placeholderForCreatedAsset];
            localIdentifier = assetPlaceholder.localIdentifier;
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                if (completion) {
                    completion(error, localIdentifier);
                }
            } else {
                NSLog(@"保存视频时出错: %@", error);
                if (completion) {
                    completion(error, nil);
                }
            }
        }];
    }];
}

+ (void)saveImageToPhotoLibrary:(UIImage *)image completion:(void (^)(NSError *error, NSString *localIdentifier))completion {
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        if (status != PHAuthorizationStatusAuthorized) {
            if (completion) {
                completion(nil, nil);
            }
            return;
        }
        
        __block NSString *localIdentifier = nil;
        
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetChangeRequest *creationRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
            PHObjectPlaceholder *assetPlaceholder = [creationRequest placeholderForCreatedAsset];
            localIdentifier = assetPlaceholder.localIdentifier;
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                if (completion) {
                    completion(error, localIdentifier);
                }
            } else {
                NSLog(@"保存图片时出错: %@", error);
                if (completion) {
                    completion(error, nil);
                }
            }
        }];
    }];
}

+ (void)fetchVideoWithLocalIdentifier:(NSString *)localIdentifier completion:(void (^)(AVAsset *videoAsset))completion {
    PHFetchResult<PHAsset *> *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil];
    if (fetchResult.count > 0) {
        PHAsset *asset = fetchResult.firstObject;
        PHVideoRequestOptions *requestOptions = [[PHVideoRequestOptions alloc] init];
        requestOptions.networkAccessAllowed = YES;

        [[PHImageManager defaultManager] requestAVAssetForVideo:asset
                                                        options:requestOptions
                                                  resultHandler:^(AVAsset *avAsset, AVAudioMix *audioMix, NSDictionary *info) {
            if (completion) {
                completion(avAsset);
            }
        }];
    } else {
        if (completion) {
            completion(nil);
        }
    }
}

@end
